package com.young.android.atomoperation;

import java.util.ArrayList;

import android.database.Cursor;

import com.young.android.common.PubFunction;
import com.young.android.dbreposit.SqliteHelper;
import com.young.android.entity.Resource;

public class OperResource {

    /**
     * 插入
     * 
     * @param r
     * @return
     */
    public static int insertResource(Resource r) {

        StringBuffer sb = new StringBuffer();
        sb.append("INSERT INTO RSS_RESOURCE( ");
        sb.append("NAME,URL)");
        sb.append(" VALUES( ");
        sb.append("'" + r.name + "',");
        sb.append("'" + r.url + "'");
        sb.append(")");
        return SqliteHelper.ExecSQL(sb.toString());

    }

    /**
     * 修改
     * 
     * @param r
     * @param name
     * @return
     */
    public static int updateResource(Resource r, String url) {

        StringBuffer sb = new StringBuffer();
        sb.append("UPDATE RSS_RESOURCE SET ");
        sb.append("NAME    ='" + r.name + "',");
        sb.append("URL      ='" + r.url + "',");
        sb.append(" WHERE URL ='" + url + "'");

        return SqliteHelper.ExecSQL(sb.toString());

    }

    /**
     * 删除
     * 
     * @param r
     * @return
     */
    public static int deleteResource(Resource r) {
        StringBuffer sb = new StringBuffer();
        sb.append("DELETE FROM RSS_RESOURCE ");
        sb.append(" WHERE URL='" + r.url + "'");

        return SqliteHelper.ExecSQL(sb.toString());
    }

    /**
     * 获取某个Resource
     * 
     * @param name
     * @return
     */
    public static Resource getResource(String name) {
        Resource r = null;
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT NAME,URL FROM RSS_RESOURCE ");
        sb.append(" WHERE NAME ='" + name + "'");

        Cursor cur = SqliteHelper.QuerySql(sb.toString());
        try {
            if ((cur != null) && (cur.getCount() > 0)) {
                cur.moveToFirst();
                r = new Resource();
                r.LoadFormCursor(cur);
            }
        } finally {
            PubFunction.CloseCursor(cur);
        }

        return r;
    }

    /**
     * 获取所有Resource
     * 
     * @param name
     * @return
     */
    public static ArrayList<Resource> getAllResources() {
        ArrayList<Resource> rList = null;
        Resource r = null;
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT NAME,URL FROM RSS_RESOURCE ");

        Cursor cur = SqliteHelper.QuerySql(sb.toString());
        try {
            if ((cur != null) && (cur.getCount() > 0)) {
                cur.moveToFirst();
                
                rList = new ArrayList<Resource>();
                do {
                    r = new Resource();
                    r.LoadFormCursor(cur);
                    rList.add(r);
                } while (cur.moveToNext());
            }
        } finally {
            PubFunction.CloseCursor(cur);
        }

        return rList;
    }
}
